package com.kzd.btgame.custom

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.btgame.R
import com.kzd.btgame.callback.AppJsInterfaceObject

fun createViewEmptyNull(
    context: Context, hint: String? = null, @DrawableRes color: Int = android.R.color.transparent,
): View = LayoutInflater.from(context).inflate(R.layout.view_empty_null, (null))
    .apply {
        layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        findViewById<TextView>(R.id.tvEmptyNullHint).text = hint
        setBackgroundResource(color)
    }

fun createViewEmptyLoad(
    context: Context, @DrawableRes color: Int = android.R.color.transparent,
): View = LayoutInflater.from(context)
    .inflate(R.layout.view_empty_load, (null))
    .apply {
        layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        setBackgroundResource(color)
    }

fun createViewGameImage(
    context: Context,
    width: Int = FrameLayout.LayoutParams.MATCH_PARENT,
    height: Int = FrameLayout.LayoutParams.WRAP_CONTENT,
): View = ImageView(context).apply {
    this.layoutParams = FrameLayout.LayoutParams(width, height)
    this.scaleType = ImageView.ScaleType.FIT_XY
}

fun createViewTextBottom(context: Context,hint: String? = null): View =
    LayoutInflater.from(context).inflate(R.layout.view_home_footer, (null)).apply {
        findViewById<TextView>(R.id.tvHomeFooterHint).text = hint
    }

fun createViewTextMove(
    context: Context, text: String, textSize: Float = 12f,
    width: Int = FrameLayout.LayoutParams.MATCH_PARENT,
    height: Int = FrameLayout.LayoutParams.WRAP_CONTENT,
    textColor: Int = R.color.black,
    @DrawableRes background: Int = R.color.white,
): View = TextView(context).apply {
    this.layoutParams = FrameLayout.LayoutParams(width, height)
    this.setPadding(0, ConvertUtils.dp2px(5f), 0, ConvertUtils.dp2px(5f))
    this.gravity = Gravity.CENTER
    this.text = text
    this.textSize = textSize
    this.setTextColor(ColorUtils.getColor(textColor))
    this.setBackgroundResource(background)
}

@SuppressLint("SetJavaScriptEnabled")
fun createViewWeb(context: Context): WebView = WebView(context).apply {
    isHorizontalScrollBarEnabled = false
    isVerticalScrollBarEnabled = false
    webViewClient = WebViewClient()
    //支持缩放，默认为true。
    settings.setSupportZoom(false)
    //调整图片至适合webview的大小
//    settings.useWideViewPort = true
    // 缩放至屏幕的大小
    settings.loadWithOverviewMode = true
    //设置默认编码
    settings.defaultTextEncodingName = "utf-8"
    //设置自动加载图片
    settings.loadsImagesAutomatically = true
    //允许访问文件
    settings.allowFileAccess = true
    //是否节点缓存
    settings.domStorageEnabled = true
    //开启javascript
    settings.javaScriptEnabled = true
    //支持通过JS打开新窗口
    settings.javaScriptCanOpenWindowsAutomatically = true
    //支持内容重新布局
    settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
    //关闭webview中缓存
    settings.cacheMode = WebSettings.LOAD_NO_CACHE
    // 设置开启定位功能
    settings.setGeolocationEnabled(true)

    settings.loadWithOverviewMode = true
}.apply {
    addJavascriptInterface(AppJsInterfaceObject(), AppJsInterfaceObject.NAME)
}